package com.yx.study.leet.primary.other;

/**
 * @Classname MissingNumber
 * @Date 2023/02/12 23:56
 * @Author by yx
 * @Description : 缺失的数字 利用异或 和完整数字异或结果便是缺失数字
 * https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnj4mt/
 */
public class MissingNumber {
    public int missingNumber(int[] nums) {
        /**
         * a^a=0 自己和自己异或为零
         * a^0=a 任何数与0异或为本身
         * */
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            res ^= nums[i]^(i+1);
        }
        return res;
    }
}
